package com.jesse.eclgy.plug.wrapper;


import cn.hutool.core.util.StrUtil;
import com.jesse.eclgy.plug.enums.FieldType;
import com.jesse.eclgy.plug.logging.Logger;
import com.jesse.eclgy.plug.logging.LoggerFactory;
import com.jesse.eclgy.plug.util.ReflectionUtils;

import java.util.*;

/**
 * @Author: jesse
 * @Date: 2023/6/3 12:45
 */
public class UpdateWrapper implements IWrapper {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 更新的列 和值
     */
    private Map<String, Object> columns;

    /**
     * 更新列对应的值
     */
    List<Object> params;
    private String tableName;

    private ConditionWrapper condition;

    public static UpdateWrapper builder()
    {
        return new UpdateWrapper();
    }

    public UpdateWrapper()
    {
        this.columns = new LinkedHashMap<>(2);
    }


    /**
     * 查询表名
     */
    public UpdateWrapper tableName(String tableName)
    {
        this.tableName = tableName;
        return this;
    }

    public UpdateWrapper set(String field, Object value)
    {
        this.columns.put(field, value);
        return this;
    }

    public UpdateWrapper set(Map<String, Object> columns)
    {
        this.columns.putAll(columns);
        return this;
    }


    public UpdateWrapper condition(Map<String, Object> conditionMap)
    {
        if (this.condition == null)
        {
            this.condition = ConditionWrapper.builder();
        }
        this.condition.condition(conditionMap);
        return this;
    }

    /**
     * 条件
     */
    public UpdateWrapper condition(ConditionWrapper condition)
    {
        this.condition = condition;
        return this;
    }

    public UpdateWrapper conditionEQ(String field, Object value)
    {
        this.condition = this.condition == null ? ConditionWrapper.builder() : this.condition.and();
        this.condition.eq(field, value);
        return this;
    }

    @Override
    public UpdateWrapper inject(Object obj)
    {
        final Map<String, Object> params = ReflectionUtils.getFieldMap(obj, FieldType.UPDATE);
        this.set(params)
                .condition(ReflectionUtils.getKeyField(obj))
                .tableName(ReflectionUtils.getTableName(obj.getClass(), false));
        return this;
    }

    @Override
    public String getSql()
    {
        StringBuilder sql = new StringBuilder();
        sql.append("UPDATE ")
                .append(tableName)
                .append(" SET ");
        this.params = new ArrayList<>();

        this.columns.forEach((k, v) -> {
            sql.append(k).append("=?,");
            this.params.add(v);
        });

        if (sql.charAt(sql.length() - 1) == ',')
        {
            sql.deleteCharAt(sql.length() - 1);
        }

        if (null != condition)
        {
            sql.append(" WHERE ").append(condition.getSql());
            this.params.addAll(condition.getParams());
        } else
        {
            this.logger.warn(StrUtil.format("sql:{},没有条件!!!", sql));
        }
        return sql.toString();
    }


    @Override
    public List<Object> getParams()
    {
        return this.params;
    }
}
